package com.ruoyi.store.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.store.mapper.WarehouseGoodsMapper;
import com.ruoyi.store.mapper.WarehouseMapper;
import com.ruoyi.store.service.IWarehouseService;
import com.ruoyi.system.api.domain.store.Warehouse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.ruoyi.common.core.utils.PageUtils.startPage;

/**
 * 仓库信息Service业务层处理
 *
 * @author wl
 * @date 2024-10-25
 */
@Service
@Slf4j
public class WarehouseServiceImpl implements IWarehouseService {
    @Autowired
    private WarehouseMapper warehouseMapper;

    @Autowired
    private WarehouseGoodsMapper warehouseGoodsMapper;

    /**
     * 查询仓库信息
     *
     * @param id 仓库信息主键
     * @return 仓库信息
     */
    @Override
    public AjaxResult selectWarehouseById(Integer id) {
        if (id == null) {
            return AjaxResult.error("仓库id不能为空");
        }
        Warehouse warehouse = warehouseMapper.selectWarehouseById(id);
        log.info("WarehouseInfoServiceImpl getWarehouseInfoById id:{}, finerWarehouse:{}", id, warehouse);
        return AjaxResult.success(warehouse);
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 查询所有仓库信息
     * @date 2024/10/26 下午11:06
     **/
    public AjaxResult listAll() {
        Warehouse warehouse = new Warehouse();
        List<Warehouse> warehouseList = warehouseMapper.selectWarehouseList(warehouse);
        log.info("WarehouseServiceImpl listAll warehouseList:{}", warehouseList);
        return AjaxResult.success(warehouseList);
    }

    /**
     * 查询仓库信息列表
     *
     * @param warehouse 仓库信息
     * @return 仓库信息
     */
    @Override
    public List<Warehouse> selectWarehouseList(Warehouse warehouse) {
        //设置分页数据
        startPage();
        List<Warehouse> warehouseList = warehouseMapper.selectWarehouseList(warehouse);
        log.info("WarehouseServiceImpl selectWarehouseList warehouseList:{}", warehouseList);
        return warehouseList;
    }

    /**
     * 新增仓库信息
     *
     * @param warehouse 仓库信息
     * @return 结果
     */
    @Override
    public AjaxResult insertWarehouse(Warehouse warehouse) {
        if (StringUtils.isBlank(warehouse.getHouseName())) {
            return AjaxResult.error("500", "仓库名称不能为空");
        }

        //校验仓库编码是否重复
        AjaxResult ajaxResult = checkHouseCode(warehouse.getHouseCode());
        if ("200".equals(ajaxResult.get("code"))) {
            return ajaxResult;
        }

        //根据当前登录用户设置
        warehouse.setCreateBy(SecurityUtils.getUsername());
        warehouse.setCreateDate(new Date());
        int addNum = warehouseMapper.insertWarehouse(warehouse);
        log.info("WarehouseInfoServiceImpl addWarehouseInfo warehouse: {} addNum:{}", warehouse, addNum);
        if (addNum > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * 修改仓库信息
     *
     * @param warehouse 仓库信息
     * @return 结果
     */
    @Override
    public AjaxResult updateWarehouse(Warehouse warehouse) {
        if (warehouse.getId() == null) {
            return AjaxResult.error("500", "仓库id不能为空");
        }
        if (StringUtils.isBlank(warehouse.getHouseName())) {
            return AjaxResult.error("500", "仓库名称不能为空");
        }
        warehouse.setModifyDate(new Date());
        warehouse.setModifyBy("admin");
        int updNum = warehouseMapper.updateWarehouse(warehouse);
        log.info("WarehouseInfoServiceImpl updateWarehouseInfo warehouse: {} updNum:{}", warehouse, updNum);
        if (updNum > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("更新失败");
    }

    /**
     * 批量删除仓库信息
     *
     * @param ids 需要删除的仓库信息主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteWarehouseByIds(Integer[] ids) {
        //校验仓库中是否有商品
        int stock = warehouseGoodsMapper.selectStockByWarehouseIds(ids);
        if (stock > 0) {
            return AjaxResult.error("仓库中存在商品，无法删除");
        }
        int delNum = warehouseMapper.deleteWarehouseByIds(ids);
        log.info("WarehouseInfoServiceImpl deleteWarehouseByIds ids: {} delNum:{}", ids, delNum);
        if (delNum > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("删除失败");
    }

    /**
     * 删除仓库信息信息
     *
     * @param id 仓库信息主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteWarehouseById(Integer id) {
        //校验仓库中是否有商品
        Integer[] ids = new Integer[]{id};
        int stock = warehouseGoodsMapper.selectStockByWarehouseIds(ids);
        if (stock > 0) {
            return AjaxResult.error("仓库中存在商品，无法删除");
        }
        int delNum = warehouseMapper.deleteWarehouseById(id);
        log.info("WarehouseInfoServiceImpl deleteWarehouseById id: {} delNum:{}", id, delNum);
        if (delNum > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("删除失败");
    }

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 检查仓库编码是否重复
     * @date 2024/10/23 下午6:04
     * @Param houseCode:
     **/
    public AjaxResult checkHouseCode(String houseCode) {
        //分类编号不能为空
        if (StringUtils.isBlank(houseCode)) {
            return AjaxResult.error("500", "仓库编码不能为空");
        }

        //根据分类编号获取分类编码数量
        int count = warehouseMapper.selectWarehouseCountByCode(houseCode);
        log.info("WarehouseInfoServiceImpl checkHouseCode houseCode: {} count:{}", houseCode, count);

        //如果数量大于0，则说明分类编码已存在
        if (count > 0) {
            return AjaxResult.error("仓库编码已存在,请重新刷新!");
        }
        return AjaxResult.success();
    }
}
